package com.wn.common.dialog

import android.app.Dialog
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ScreenUtils
import com.wn.common.databinding.DialogMessageBinding
import com.wn.framework.base.BaseDialog
import com.wn.framework.base.BaseDialog.AnimStyle.TOAST
import com.wn.framework.base.BaseDialogFragment
import com.wn.framework.ext.onClick

/**
 * @Author: wn
 * @Date: 2024/12/17 16:29
 * @Description: 普通弹框
 */
class MessageDialog {

  class Builder(
    activity: FragmentActivity,
  ) : BaseDialogFragment.Builder<Builder>(activity) {
    /**
     * 此处为实例 的 XML 布局
     */
    private val mBinding: DialogMessageBinding =
      DialogMessageBinding.inflate(LayoutInflater.from(activity))

    private var mAutoDismiss = true // 设置点击按钮后自动消失

    init {
      // 绑定 实例 布局
      setContentView(mBinding.root)
      setWidth((ScreenUtils.getScreenWidth() * 0.8).toInt())
      setAnimStyle(TOAST)
      setGravity(Gravity.CENTER)
    }

    fun setTitle(resId: Int): Builder {
      return setTitle(getText(resId))
    }

    fun setTitle(text: CharSequence?): Builder {
      mBinding.tvDialogMessageTitle.text = text
      return this
    }

    fun setMessage(resId: Int): Builder {
      return setMessage(getText(resId))
    }

    fun setMessage(text: CharSequence?): Builder {
      mBinding.tvDialogMessageMessage.text = text
      return this
    }

    fun setCancel(resId: Int): Builder {
      return setCancel(getText(resId))
    }

    fun setCancel(text: CharSequence?): Builder {
      mBinding.tvDialogMessageCancel.text = text
      val isEmpty = text.isNullOrEmpty()
      mBinding.tvDialogMessageCancel.isVisible = !isEmpty
      mBinding.vDialogMessageLine.visibility = if (isEmpty) View.GONE else View.VISIBLE
      mBinding.tvDialogMessageConfirm.setBackgroundResource(if (isEmpty) com.wn.framework.R.drawable.dialog_message_one_button else com.wn.framework.R.drawable.dialog_message_right_button)
      return this
    }

    fun setConfirm(resId: Int): Builder {
      return setConfirm(getText(resId))
    }

    fun setConfirm(text: CharSequence?): Builder {
      mBinding.tvDialogMessageConfirm.text = text
      return this
    }

    fun setConfirmTxtColor(@ColorInt color: Int): Builder {
      mBinding.tvDialogMessageConfirm.setTextColor(color)
      return this
    }

    fun setCancelTextColor(@ColorInt color: Int): Builder {
      mBinding.tvDialogMessageCancel.setTextColor(color)
      return this
    }

    fun setAutoDismiss(dismiss: Boolean): Builder {
      mAutoDismiss = dismiss
      return this
    }

    // 传递 lambda 参数获取 dialog 对象，进行 dismiss 操作 以及其他 确认逻辑操作
    fun setonConfirmListener(onConfirm: (dialog: Dialog?) -> Unit): Builder {
      mBinding.tvDialogMessageConfirm.onClick {
        onConfirm.invoke(dialog)
      }
      return this
    }

    // 传递 lambda 参数获取 dialog 对象，进行 dismiss 操作
    fun setonCancelListener(onCancel: (dialog: Dialog?) -> Unit): Builder {
      mBinding.tvDialogMessageCancel.onClick {
        onCancel.invoke(dialog)
      }
      return this
    }

    override fun create(): BaseDialog {
      // 如果标题为空就隐藏
      if (mBinding.tvDialogMessageTitle.text.isNullOrEmpty()) {
        mBinding.tvDialogMessageTitle.visibility = View.GONE
      }
      // 如果内容为空就抛出异常
//           if (TextUtils.isEmpty(mMessageView.getText())) {
//                //throw new IllegalArgumentException("Dialog message not null");
//           }
      return super.create()
    }
  }
}